package com.jnu.keepaccounts.manage;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.jnu.keepaccounts.R;
import com.jnu.keepaccounts.data.Data;
import com.jnu.keepaccounts.data.DataBank;
import com.jnu.keepaccounts.data.DataTiaoMu;
import com.jnu.keepaccounts.data.DataTou;
import com.jnu.keepaccounts.history.DayFragment;

import java.util.ArrayList;
import java.util.Arrays;

public class Show extends AppCompatActivity {
    private ArrayList<Data> datas= new ArrayList<>(),tous = new ArrayList<>() ;
    private String name;
    private double shou,zhi;
    private int pictureId,total;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);
        ActionBar actionBar = getSupportActionBar() ;
        if(actionBar!=null){
            actionBar.hide();
        }
        Intent intent = getIntent();
        name = intent.getStringExtra("name");
        pictureId = intent.getIntExtra("pictureId",0);

        ImageButton imageButton = findViewById(R.id.back);
        imageButton.setOnClickListener(v -> finish());
        TextView textView = findViewById(R.id.name);
        textView.setText(name);

        ImageView imageView = findViewById(R.id.picture);
        imageView.setImageResource(pictureId);

        tongji();
        TextView textView1 = findViewById(R.id.num);
        textView1.setText("共"+total+"笔");
        TextView textView2 = findViewById(R.id.jieyu);
        textView2.setText((shou+zhi)+"");
        TextView textView3 = findViewById(R.id.shouru);
        textView3.setText(shou+"");
        TextView textView4 = findViewById(R.id.zhichu);
        textView4.setText(zhi+"");

        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        MyNewAdapter myNewAdapter = new MyNewAdapter(datas);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(myNewAdapter);

    }
    @SuppressLint("WrongConstant")
    public void tongji(){
        DataBank dataBank = new DataBank(getApplicationContext());
        for(Data data:dataBank.loadData()){
            if(data.getFlag() == 1){
                if(((DataTiaoMu)data).getXiangMu().equals(name)){
                    datas.add(data);
                }
            }
        }
        total = datas.size();
        int year=0 ,month=0 ,day=0;
        for(Data data:datas ){
            if(year!=data.getYear()||month!=data.getMonth()||day!=data.getDay()){
                year=data.getYear();
                month=data.getMonth();
                day=data.getDay();
                tous.add(new DataTou(2,year,month,day));
                ((DataTou)tous.get(tous.size()-1)).cal((DataTiaoMu) data,1);
            }
            else {
                ((DataTou)tous.get(tous.size()-1)).cal((DataTiaoMu) data,1);
            }
            if(((DataTiaoMu) data).getShuE()>0){
                shou+=((DataTiaoMu) data).getShuE();
            }
            else zhi+=((DataTiaoMu) data).getShuE();
        }
        datas.addAll(tous);
        sort();
        if(datas.size()== 0){
            TextView textView = findViewById(R.id.empty);
            textView.setVisibility(0);
        }
    }
    public void sort(){
        Object[] tem = this.datas.toArray();
        Arrays.sort(tem,(e1, e2)->{
            if(((Data)e1).getYear()>((Data)e2).getYear())return -1;
            else if(((Data)e1).getYear()==((Data)e2).getYear()) {
                if(((Data) e1).getMonth()>((Data) e2).getMonth())return -1;
                else if(((Data) e1).getMonth()==((Data) e2).getMonth()){
                    if(((Data) e1).getDay()>((Data) e2).getDay())return -1;
                    else if(((Data) e1).getDay()==((Data) e2).getDay()){
                        if(((Data) e1).getFlag()> ((Data) e2).getFlag() )return -1;
                        else if(((Data) e1).getFlag() == ((Data) e2).getFlag() )return 0;
                        else return 1;
                    }
                    else return 1;
                }
                else return 1;
            }
            else return 1;
        });
        ArrayList<Data> data = new ArrayList<>();
        this.datas.clear();
        for(Object object:tem){
            this.datas.add((Data) object);
        }
    }
    public class MyNewAdapter extends RecyclerView.Adapter<MyNewAdapter.ViewHolder>{
        ArrayList<Data> datas;

        MyNewAdapter(ArrayList<Data> datas){
            this.datas = datas;
        }
        public int getItemViewType(int position){
            return datas.get(position).getFlag();
        }
        @NonNull
        @Override
        public MyNewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            if(viewType==2)return new MyNewAdapter.TouHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.tou,parent,false));
            else return new MyNewAdapter.TiaoMuHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.tiaomu,parent,false));
        }

        @SuppressLint("WrongConstant")
        @Override
        public void onBindViewHolder(@NonNull MyNewAdapter.ViewHolder holder, int position) {
            Data data = datas.get(position);
            if(data.getFlag()==2){

                DataTou dataTou = (DataTou)data;
                MyNewAdapter.TouHolder touHolder = (MyNewAdapter.TouHolder)holder;
                touHolder.date.setText(dataTou.getYear()+"-"+ dataTou.getMonth()+"-"+dataTou.getDay()+"");
                touHolder.shou.setText(dataTou.getShou()+"");
                touHolder.zhi.setText(Math.abs(dataTou.getZhi())+"");
            }
            else {
                DataTiaoMu dataTiaoMu = (DataTiaoMu) data;
                MyNewAdapter.TiaoMuHolder tiaoMuHolder = (MyNewAdapter.TiaoMuHolder) holder;
                tiaoMuHolder.image.setImageResource(dataTiaoMu.getImage());
                tiaoMuHolder.xiangmu.setText(dataTiaoMu.getXiangMu());
                tiaoMuHolder.laiyuan.setText(dataTiaoMu.getLaiYuan());
                tiaoMuHolder.shue.setText(dataTiaoMu.getShuE()+"");
                if(dataTiaoMu.getYusuan() == 1){
                    tiaoMuHolder.yusuan.setVisibility(0);
                }
            }
        }

        @Override
        public int getItemCount() {
            return datas.size();
        }
        public class ViewHolder extends RecyclerView.ViewHolder {
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
            }
        }
        public class TouHolder extends MyNewAdapter.ViewHolder {
            TextView date;
            TextView shou;
            TextView zhi;

            public TouHolder(@NonNull View itemView) {
                super(itemView);
                date = (TextView) itemView.findViewById(R.id.textview_date);
                shou = (TextView) itemView.findViewById(R.id.textview_shou);
                zhi = (TextView) itemView.findViewById(R.id.textview_zhi);
            }
        }
        public class TiaoMuHolder extends MyNewAdapter.ViewHolder  {
            ImageView image,yusuan;
            TextView xiangmu, laiyuan, shue;

            public TiaoMuHolder(@NonNull View itemView) {
                super(itemView);
                image = (ImageView) itemView.findViewById(R.id.imageview);
                xiangmu = (TextView) itemView.findViewById(R.id.textview_xiangmu);
                laiyuan = (TextView) itemView.findViewById(R.id.textview_laiyuan);
                shue = (TextView) itemView.findViewById(R.id.textview_shue);
                yusuan = (ImageView) itemView.findViewById(R.id.yusuan);
            }
        }
    }
}